//
//  QTQualityInfoViewController.m
//  qdqts
//
//  Created by jonn on 14-6-23.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTQualityInfoViewController.h"
#import "QTQualityListTableViewCell.h"
#import "QTInfoDetailViewController.h"
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"
#import "Global.h"
#import "MJRefresh.h"

@interface QTQualityInfoViewController ()

@end

@implementation QTQualityInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


+ (QTQualityInfoViewController *)shareInstance {
    static QTQualityInfoViewController *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance=[[self alloc] init];
    });
    return _instance;
}

-(void)setValue:(NSString *)puidType
{
    _puidType=puidType;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    _tmpArray= [[NSMutableArray alloc]init];
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,NAVBAR_HEIGHT+54,SCREEN_WIDTH,SCREEN_HEIGHT-NAVBAR_HEIGHT-54) style:UITableViewStylePlain];
    
    _tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor=GRAYCOLOR;
    _tableView.separatorStyle=NO;
    [self.view addSubview:_tableView];
    [_failure setHidden:YES];
    [self.activity startAnimating];
    [self setupRefresh];
    _num = @1;
    [self loadData];
    [self.view bringSubviewToFront:_failure];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tmpArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //UITableViewCell *cell;
    static NSString *qualityCellStr =@"qualityCell";
    
    //自定义cell
    QTQualityListTableViewCell *infoCell = (QTQualityListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:qualityCellStr];
    if(infoCell ==nil) {
        NSArray *nibArray = [[NSBundle mainBundle]loadNibNamed:@"QTQualityListTableViewCell"owner:self options:nil];
        infoCell = (QTQualityListTableViewCell *)[nibArray objectAtIndex:0];
    }
    infoCell.cellTitle.text=((QTArticle *)[_tmpArray objectAtIndex:indexPath.row]).articleTitle;
    infoCell.cellAbstract.text=((QTArticle *)[_tmpArray objectAtIndex:indexPath.row]).articleAbstract;
    NSString *thumbImgPath=((QTArticle *)[_tmpArray objectAtIndex:indexPath.row]).thumbImgPath;
    NSURL *url=[NSURL URLWithString:[qsServerUrl stringByAppendingString:thumbImgPath==nil?@"":thumbImgPath]];
    [infoCell.cellImage setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
        if (image == nil) {
            [infoCell.cellImage setImage:[UIImage imageNamed:@"暂无图片.png"]];
        }
    } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray ];
    infoCell.backgroundColor=GRAYCOLOR;
    return infoCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QTInfoDetailViewController *infoDetailViewController=[[QTInfoDetailViewController alloc] init];
    infoDetailViewController.puid=((QTArticle *)[_tmpArray objectAtIndex:indexPath.row]).puid;
    [self.navigationController pushViewController:infoDetailViewController animated:YES];

}
#pragma mark - Table view delegate


/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态

- (void)footerRereshing
{
    //加载下一组数据
    [self loadData];
}

-(void)loadData{
    NSLog(@"当前加载数据的起始值：%@",_num);

    [[QSClient instance] loadNewsList:@4 startIndex:_num maxResults:@10 productTypePuid:_puidType completionBlock:^(NSArray *articleList, NSError *error) {
        NSLog(@"%@",_tmpArray);
        [_tmpArray addObjectsFromArray:articleList];
        NSLog(@"%@",_tmpArray);
        [self.activity stopAnimating];
        if (error) {
//            self.failure.text=@"无法连接服务器，请检查网络";
//            [self.failure setHidden:NO];
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                          message:[NSString stringWithFormat:@"无法连接服务器，请稍后在试",nil]
                                                         delegate:self
                                                cancelButtonTitle:@"确定"
                                                otherButtonTitles:nil, nil];
            [alert show];
        }
        else if ([_tmpArray count]==0) {
            _failure.hidden=NO;
        }else{
            _failure.hidden=YES;
            _tableView.separatorStyle=YES;
            [self.tableView reloadData];
            _num=@(_num.intValue+10);
        }
        
        //结束所有的刷新状态
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];

    }];
}

@end
